package fr.charlott.view;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.google.code.geocoder.AdvancedGeoCoder;
import com.google.code.geocoder.GeocoderRequestBuilder;
import com.google.code.geocoder.model.GeocodeResponse;
import com.google.code.geocoder.model.GeocoderRequest;
import com.google.code.geocoder.model.GeocoderResult;

import fr.charlott.business.dto.ClienteDTO;
import fr.charlott.business.dto.HotesseDTO;
import fr.charlott.business.service.ClienteService;
import fr.charlott.business.service.HotesseService;

@Component("localiseClienteControler")
@Scope("view")
public class LocaliseClienteControler extends CharlottControler {

	private static final long serialVersionUID = 1L;

	private final Logger logger = LoggerFactory.getLogger(LocaliseClienteControler.class);
	/**
	 * Liste des clientes
	 */
	private List<ClienteDTO> clientes;
	
	/**
	 * Liste des hotesses
	 */
	private List<HotesseDTO> hotesses;

	/**
	 * MapModel
	 */
	private MapModel mapModel;
	
	/**
	 * Marker
	 */
	private Marker marker;
	
	/**
	 * Google geocoder
	 */
	final AdvancedGeoCoder geocoder = new AdvancedGeoCoder();
	
	public LocaliseClienteControler() {
		super();
	}

	@Autowired(required = true)
	public LocaliseClienteControler(ClienteService clienteService, HotesseService hotesseService) {
		this();
		mapModel = new DefaultMapModel();
		//geocoder.getHttpClient().getHostConfiguration().setProxy("10.6.8.1", 3128);
		
		// Chargement des clientes
		clientes = clienteService.chargerToutesLesClientes();
		
		// Chargement des hotesses
		hotesses = hotesseService.chargerToutesLesHotesses();
		
		for (ClienteDTO cliente : clientes) {
			if (StringUtils.isNotEmpty(cliente.getAdresse().getVille())) {
				try{
					GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(cliente.getAdresse().toString()).setLanguage("fr").getGeocoderRequest();
					GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
					List<GeocoderResult> results = geocoderResponse.getResults();
					double latitude = results.get(0).getGeometry().getLocation().getLat().doubleValue();
					double longitude = results.get(0).getGeometry().getLocation().getLng().doubleValue();
					
					LatLng coord = new LatLng(latitude, longitude);
					mapModel.addOverlay(new Marker(coord, cliente.toString() + " - " + cliente.getAdresse(), null, "http://maps.google.com/mapfiles/ms/micons/yellow-dot.png"));
				}catch (Exception e){
					logger.error(e.getMessage());
				}
			}
		}
		
		for (HotesseDTO hotesse : hotesses) {
			if (StringUtils.isNotEmpty(hotesse.getAdresse().getVille())) {
				try{
					GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(hotesse.getAdresse().toString()).setLanguage("fr").getGeocoderRequest();
					GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
					List<GeocoderResult> results = geocoderResponse.getResults();
					double latitude = results.get(0).getGeometry().getLocation().getLat().doubleValue();
					double longitude = results.get(0).getGeometry().getLocation().getLng().doubleValue();
					
					LatLng coord = new LatLng(latitude, longitude);
					mapModel.addOverlay(new Marker(coord, hotesse.toString() + " - " + hotesse.getAdresse(), null, "http://maps.google.com/mapfiles/ms/micons/blue-dot.png"));
				}catch (Exception e){
					logger.error(e.getMessage());
				}
			}
		}
		
	}

	// getters - setters

	public MapModel getMapModel() {  
        return mapModel;  
    }  
      
    public void onMarkerSelect(OverlaySelectEvent event) {  
        marker = (Marker) event.getOverlay();  
    }  
      
    public Marker getMarker() {  
        return marker;  
    }  

}
